package myTest;

import java.io.*;

public class map{
	static int busCount=0;
	static int stationCount=0;
	
	public static int locationOfStation(String[] station, String str){
		int i=0;
		while(station[i] != null){
			if(station[i].compareTo(str)==0)
				return i;
			i++;
		}
		station[i]=str;
		stationCount++;
		return i;
	}
	
	public static void main(String[] args){
		String[] bus = new String[400];
		String[] station = new String[4000];
		String ReadFilePath="C:\\Users\\ChengXin\\Desktop\\bus.txt";
		int[][] map=new int[400][4000];
		
		try{
			String s;
			BufferedReader in = new BufferedReader(new FileReader(ReadFilePath));
			while((s = in.readLine()) != null){
				String array[] = s.split(" ");
				int locationPre=locationOfStation(station,array[1]);
				int locationNext;
				for(int i=1; i<(array.length-1); i++){
					locationNext=locationOfStation(station,array[i+1]);
					map[busCount][locationPre]=locationNext;
					locationPre=locationNext;
				}
				//map[busCount][locationPre]=locationPre;
				bus[busCount++]=array[0];
			}
			String WriteFilePath="C:\\Users\\ChengXin\\Desktop\\busANDstation.txt";
			FileOutputStream out=new FileOutputStream(WriteFilePath);
			BufferedWriter buffWriter = new BufferedWriter(new OutputStreamWriter(out));

			buffWriter.write((busCount-1)+"");
            for(int i=1; i<busCount; i++)
            	buffWriter.write(" "+bus[i]);
            buffWriter.write("\r\n");
            
            buffWriter.write((stationCount-1)+"");
            for(int i=1; i<stationCount; i++)
            	buffWriter.write(" "+station[i]);
            
            buffWriter.flush();
            
            String MapFilePath="C:\\Users\\ChengXin\\Desktop\\map.txt";
            out=new FileOutputStream(MapFilePath);
            buffWriter=new BufferedWriter(new OutputStreamWriter(out));
            for(int i=1; i<busCount; i++){
            	for(int j=1; j<map[i].length; j++)
            		buffWriter.write(map[i][j]+" ");
            	buffWriter.write("\r\n");
            }
            buffWriter.flush();
		}
		catch(Exception e){
			System.out.println(e.toString());
		}
	}
}